package com.xiaoyu.merchant.ui.fragment.store;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xiaoyu.commonlib.ui.base.BaseFragment;
import com.xiaoyu.commonlib.ui.widget.AutofitHeightViewPager;
import com.xiaoyu.merchant.MerchantConstants;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.loader.BannerImageLoader;
import com.xiaoyu.merchant.model.StoreDetailBean;
import com.xiaoyu.merchant.ui.activity.PhotoViewActivity;
import com.xiaoyu.merchant.ui.activity.wallet.WalletActivity;
import com.xiaoyu.merchant.ui.fragment.main.StoreFragment;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商家fragment
 */
public class StoreIntroductionFragment extends BaseFragment {
    private View mView;

    @BindView(R.id.fragment_store_banner)
    Banner mBanner;//轮播图
    @BindView(R.id.fragment_store_name)
    TextView nameTxt;//商家名称
    @BindView(R.id.fragment_store_describe)
    TextView describeTxt;//商家介绍
    @BindView(R.id.fragment_store_address)
    TextView addressTxt;//详细地址

    private List<String> mBannerList = new ArrayList<>();//轮播图数据

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_store_introduction, container, false);
            ButterKnife.bind(this, mView);
            initView();
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        return mView;
    }

    private void initView() {
        Bundle bundle = getArguments();
        int pos = bundle.getInt(AutofitHeightViewPager.POSITION);
        StoreFragment.mViewPager.setViewPosition(mView,pos);
        StoreDetailBean.DataBean data = (StoreDetailBean.DataBean) bundle.getSerializable(MerchantConstants.MERCHANT_INFO);
        mBannerList.clear();
        mBannerList.addAll(data.getStorebanner());

        //轮播图
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        mBanner.setViewPagerIsScroll(true);
        mBanner.isAutoPlay(true);
        mBanner.setDelayTime(2000);
        mBanner.setImageLoader(new BannerImageLoader());
        mBanner.setImages(mBannerList);
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(getActivity(),PhotoViewActivity.class);
                intent.putExtra(MerchantConstants.IMAGER_LIST_URL, (Serializable) mBannerList);
                intent.putExtra(MerchantConstants.IMAGER_CLICK_POS, position);
                startActivity(intent);
            }
        });
        mBanner.start();

        nameTxt.setText(data.getStorename());//商家名称
        describeTxt.setText(data.getStoreintroduce());//商家介绍
        addressTxt.setText(data.getStoreaddresss());//详细地址
    }

    //我的钱包
    @OnClick(R.id.fragment_my_wallet_relative)
    public void myWalletClick(){
        startActivity(new Intent(getActivity(), WalletActivity.class));
    }
}
